package com.yj.oa.mapper;

import com.yj.oa.entity.system.OAUserConfig;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/18
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface OAUserConfigMapper {

    @Insert("INSERT INTO oa_user_config (user_id, username, account, user_number, cellphone, department_id, is_leader, division_name)" +
            "VALUES(#{item.user_id}, #{item.username}, #{item.account}, #{item.user_number}, " +
            "#{item.cellphone}, #{item.department_id}, #{item.is_leader}, #{item.division_name}) " +
            "ON DUPLICATE KEY " +
            "UPDATE user_id = #{item.user_id}, username = #{item.username}, account = #{item.account}, " +
            "user_number = #{item.user_number}, cellphone = #{item.cellphone}, department_id = #{item.department_id}, " +
            "is_leader = #{item.is_leader}, division_name = #{item.division_name}")
    void insertUpdate(@Param("item") OAUserConfig item);

    @Delete("<script>" +
            "DELETE FROM oa_user_config " +
            "WHERE user_id IN " +
            "<foreach item = 'item' collection = 'list' open = '(' separator = ', ' close = ') '>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void batchDelete(@Param("list") List<String> list);

    @Select("SELECT * FROM oa_user_config WHERE user_id = #{user_id}")
    OAUserConfig getByUserID(@Param("user_id") String user_id);

    @Select("SELECT * FROM oa_user_config " +
            "WHERE department_id = #{department_id} AND is_leader = 0 " +
            "ORDER BY user_id")
    List<OAUserConfig> subUserForLeader(@Param("department_id") Long department_id);

    @Select("SELECT * FROM oa_user_config " +
            "WHERE department_id = #{department_id} " +
            "ORDER BY user_id")
    List<OAUserConfig> subUserForDepartment(@Param("department_id") Long department_id);

    @Select("SELECT * FROM oa_user_config " +
            "WHERE department_id IN " +
            "(SELECT department_id FROM oa_department_config WHERE superior_department_id = #{superior_department_id} AND department_id != #{department_id}) " +
            "ORDER BY user_id")
    List<OAUserConfig> subUserForAuthority(@Param("superior_department_id") Long superior_department_id,
                                           @Param("department_id") Long department_id);
}